package edu.hbsi.demo4;
import java.util.Scanner;
public class PasswordCheck {
    public static void checkPassword(String password) throws UnSafePasswordException {
        if (password == null || password.length() <= 6) {
            throw new UnSafePasswordException("密码长度必须大于6。");
        }
        boolean hasDigit = false;
        boolean hasUpper = false;
        boolean hasLower = false;
        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);
            if (Character.isDigit(c)) {
                hasDigit = true;
            } else if (Character.isUpperCase(c)) {
                hasUpper = true;
            } else if (Character.isLowerCase(c)) {
                hasLower = true;
            }
        }
        if (!hasDigit || !hasUpper || !hasLower) {
            throw new UnSafePasswordException("密码必须包含数字、大写字母和小写字母。");
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        try {
            checkPassword(password);
            System.out.println("密码安全");
        } catch (UnSafePasswordException e) {
            System.out.println(e.getMessage());
        } finally {
            scanner.close();
        }
    }
}
